package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.UnLikesBehaviorDto;
import com.heima.behavior.entity.ApUnlikesBehavior;
import com.heima.behavior.mapper.ApUnlikesBehaviorMapper;
import com.heima.behavior.service.IApUnlikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-01-22
 */
@Service
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements IApUnlikesBehaviorService {

    @Override
    public ResponseResult unlike(UnLikesBehaviorDto dto) {
        //根据入参查询数据
        LambdaQueryWrapper<ApUnlikesBehavior> qw = new LambdaQueryWrapper<>();
        qw.eq(ApUnlikesBehavior::getArticleId, dto.getArticleId());
        qw.eq(ApUnlikesBehavior::getEntryId, dto.getEntryId());
        ApUnlikesBehavior one = this.getOne(qw);
        //判断是否已经存在该数据
        if (one == null) {
            // 点击不喜欢 --新增一条数据
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            BeanUtils.copyProperties(dto, apUnlikesBehavior);
            this.save(apUnlikesBehavior);
        } else{
            this.remove(qw);
        }
        return ResponseResult.okResult();
    }
}
